<?php

namespace app\miniprogram\controller;

class ProductSpecs extends Base {
	protected $process = ['Info', 'tysj\\ProductSpecs', null, ['append' => []]];
    protected $noNeedLogin = ['list4store'];

    //可获取规格详情
    public function info() {
        $this->ajax_process(function($process, $user) {
            return $process->info($user);
        });
    }

    //根据产品获取规格列表
    public function list4store() {
        $this->ajax_process(function($process, $user) {
            return $process->list(function($model) {
                $pid = input('param.pid', -1);
                $ids = loadModel('tysj\\ProductSpecsNexus')->where(['pid' => $pid])->column('psid');
                $model->whereIn('id', $ids);
            });
        });
    }
	
	//产品规格列表
	public function private_list() {
        $param = input('param.');

		$this->ajax_process(function($process, $user) use ($param) {
            return $process->private_list($user, function($model) use ($param) {
                $order = isset($param['order']) ? $param['order'] : 'createtime';
                $orderType = isset($param['order_type']) ? $param['order_type'] : 'desc';
                $model->order($order . ' ' . $orderType);
            });
        });
	}

    //增减产品规格库存
    public function set_inventory() {
        $user = $this->get_login()->getCurrentUser();
        $ids = $this->request->param('ids');
        $values = $this->request->param('values');

        $data = [
            'uid'    => $user['id'],
            'ids'    => explode(',', $ids),
            'values' => explode(',', $values)
        ];
        $validate = loadValidate('tysj\\SetInventory');
        if(!$validate->scene('update')->check($data)) {
            $this->error($validate->getError());
        }
        $model = loadModel('tysj\\ProductSpecs');
        $ids = $data['ids'];
        $values = $data['values'];
        try {
            $model->startTrans();
            foreach($ids as $k => $v) {
                $row = loadOnly('Model')->lockFind('tysj\\ProductSpecs', $v);
                $row->setInc('inventory', $values[$k]);
                $row = $model->where(['id' => $v])->find();
                if(!$validate->scene('over')->check($row)) {
                    throw new \Exception($v . $validate->getError());
                }
            }
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

	//产品规格获取
	public function get_options() {
		$this->ajax_process(function($process, $user) {
            return $process->getOptions($user);
        }, ['GetOptions', 'tysj\\ProductSpecs', 'ProductSpecs']);
	}

	//创建编辑规格
    public function edit() {
        $this->ajax_process(function($process, $user) {
            return $process->save($user);
        });
    }

    //删除编辑规格
    public function del() {
    	$this->ajax_process(function($process, $user) {
            return $process->delAll($user);
        });
    }
}